package pbft

import (
	"blockchain/public/entity"
	"blockchain/public/function"
	"encoding/json"
)

type block struct {
	prehash [32]byte
	priseq int
	trades [TradeSize] entity.Trade
	height int
}

func finish(){
	var b block
	b.trades=getResult()
	b.priseq=primarySeq
	b.height=currentTradeSeq
	//fmt.Println(b.trades[9].Result)
	blockChain[currentRound]=b
	chanFinish<-true
	close(chanFinish)
}

func getResult()[TradeSize]entity.Trade{
	var replys [N*2/3+1]replyMessage
	var tradesLast [TradeSize]entity.Trade
	count:=0
	for data:=range chanRecReply{
		trades:=data.trades
		d,_:=json.Marshal(trades)
		res:=function.VerifySign(d,data.sign,data.nodeSeq)
		if res{
			replys[count]=data
			count++
		}
	}

	var counter[N*2/3+1]int
	for i:=0;i<N*2/3+1;i++{
		counter[i]=1
		for j:=0;j<N*2/3+1;j++{
			if replys[i].trades==replys[j].trades{
				counter[i]++
			}
		}
	}

	for i:=0;i<N*2/3+1;i++{
		if counter[i]>N/3{
			tradesLast=replys[i].trades
		}
	}
	return tradesLast
}
